package com.wgh.inventory.service.impl;

import com.wgh.inventory.entity.Inventory;
import com.wgh.inventory.mapper.InventoryMapper;
import com.wgh.inventory.service.InventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import io.seata.spring.annotation.GlobalTransactional;

import java.util.List;

/**
 * 库存服务实现类
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
    
    @Autowired
    private InventoryMapper inventoryMapper;
    
    @Override
    public Inventory getInventoryBySkuId(Long skuId) {
        return inventoryMapper.selectBySkuId(skuId);
    }
    
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean decreaseInventory(Long skuId, Integer quantity) {
        // 检查库存是否充足
        if (!checkInventory(skuId, quantity)) {
            return false;
        }
        
        // 扣减库存
        return inventoryMapper.decreaseInventory(skuId, quantity) > 0;
    }
    
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean increaseInventory(Long skuId, Integer quantity) {
        return inventoryMapper.increaseInventory(skuId, quantity) > 0;
    }
    
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean lockInventory(Long skuId, Integer quantity) {
        // 检查库存是否充足
        if (!checkInventory(skuId, quantity)) {
            return false;
        }
        
        // 锁定库存
        return inventoryMapper.lockInventory(skuId, quantity) > 0;
    }
    
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean unlockInventory(Long skuId, Integer quantity) {
        return inventoryMapper.unlockInventory(skuId, quantity) > 0;
    }
    
    @Override
    public boolean checkInventory(Long skuId, Integer quantity) {
        return inventoryMapper.checkInventory(skuId, quantity);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWarningQuantity(Long skuId, Integer warningQuantity) {
        Inventory inventory = inventoryMapper.selectBySkuId(skuId);
        if (inventory != null) {
            inventory.setWarningStock(warningQuantity);
            return this.updateById(inventory);
        }
        return false;
    }
    
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDecreaseInventory(Long[] skuIds, Integer[] quantities) {
        if (skuIds == null || quantities == null || skuIds.length != quantities.length) {
            return false;
        }
        
        // 批量扣减库存
        for (int i = 0; i < skuIds.length; i++) {
            if (!decreaseInventory(skuIds[i], quantities[i])) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public List<Inventory> getInventoryWarningList() {
        // 查询库存预警列表
        // 这里可以根据实际需求实现，例如查询库存低于预警值的商品
        return this.list();
    }
}